estimation_data <-
  estimation_data %>%
  mutate(gender = case_when(V_1 == "Laki-laki" ~ 1,
                            V_1 == "Perempuan" ~ 0,
                            TRUE ~ NA_real_)) %>%
  mutate(age = V_2)  %>%
  mutate(college_diploma = case_when(V_3 == "Sekolah Menengah Atas (SMA) atau sederajat" ~ 0,
                                     V_3 == "Diploma (D1/D2/D3)" ~ 1,
                                     V_3 == "S1" ~ 1,
                                     V_3 == "S2" ~ 1, 
                                     V_3 == "S3" ~ 1,
                                     TRUE ~ NA_real_))  %>%
  group_by(ID) %>%
  mutate(sum_wave = sum(A)) %>%
  ungroup() %>%
  mutate(post_election_attrit = case_when(JML == "Hanya diwawancara sekali" & sum_wave %in% c(2) ~ 1,
                                          JML == "Diwawancara 2 kali (pada survei gelombang I & II atau I & III atau II & III)" & sum_wave == 3 ~ 1,
                                          TRUE ~ 0))


predict_mod <- lm(post_election_attrit ~ validated_election_outcome + age + gender + college_diploma + islam_recode_plot + list_position_recode_plot + islam_party_recode_plot, data = estimation_data)
p_treatment <- predict(predict_mod, newdata = estimation_data)
estimation_data$p_treatment <- p_treatment

estimation_data <-
  estimation_data %>%
  mutate(IPW = case_when(post_election_attrit == 1 ~ 1/p_treatment,
                         post_election_attrit == 0 ~ 1/(1-p_treatment)))


mod1 <- fixest::feols(avg_score_democ_support ~ pre_post*validated_election_outcome, data = estimation_data, cluster = ~ID)
mod1a <- fixest::feols(avg_score_democ_support ~ pre_post*validated_election_outcome, data = estimation_data, cluster = ~ID, weights = ~IPW)

mod2 <- fixest::feols(avg_score_trust_elect ~ pre_post*validated_election_outcome, data = estimation_data, cluster = ~ID)
mod2b <- fixest::feols(avg_score_trust_elect ~ pre_post*validated_election_outcome, data = estimation_data, cluster = ~ID, weights = ~IPW)



mods <- list(mod2, mod2b, mod1, mod1a)

table_df <-
  modelsummary(mods,
               stars = c("*" = 0.1, "**" = 0.05, "***" = 0.01),
               coef_map = c(
                 "pre_post" = "Post Election",
                 "validated_election_outcomeNot Elected" = "Outcome: Lost",
                 "pre_post:validated_election_outcomeNot Elected" = "Post x Lost"
               ),
               gof_map = c("nobs", "r.squared"),
               output = "data.frame"
  ) %>%
  mutate(term = ifelse(statistic == "std.error", "", term)) %>%
  select(-c(part, statistic)) %>%
  mutate_all(as.character)

fe_row <- tibble::tibble(
  term = "IPW",
  !!!setNames(rep(c("\\xmark", "\\checkmark"), 2), names(table_df)[-1])
)

# Append the new row to the table
table_df <- bind_rows(table_df, fe_row)

# Append the new row to the table
latex_table <-
  table_df %>%
  kableExtra::kable("latex",
                    booktabs = T,
                    longtable = T,
                    escape = F,
                    cap = "\\label{tab:ipw_table}Main Analysis, Inverse Propensity Weights",
                    padding = 0,
                    col.names = linebreak(c("", "(1)", "(2)", "(3)", "(4)"), c("l", rep("c", 4))),
                    align = c("l", rep("c", 4)),
                    linesep = "") %>%
  kableExtra::row_spec(6, hline_after = T) %>%
  kableExtra::kable_styling(
    font_size = 9,
    latex_options = c("scale_down",
                      "hold_position",
                      "striped")) %>%
  
  kableExtra::add_header_above(
    header = c(
      " " = 1,
      "Trust in Elec." = 2,
      "Supp. for Dem." = 2
    ),
    align = c("l", rep("c", 4))
  )

cat("\\renewcommand{\\arraystretch}{0.9}\n", file = "./outputs/tables/table_a6.tex")
cat(latex_table, file = "./outputs/tables/table_a6.tex", append = TRUE)





